package com.yxy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.yxy.common.Const;
import com.yxy.dao.SportDao;
import com.yxy.entity.Sport;

import com.yxy.service.SportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;




/**
 *
 * SportServiceImpl 实现服务类
 *
 * @version
 *
<pre>
 * Author       Version         Date            Changes
 * 邓文俊    1.0  2018-01-11 16:28:27	 	Created
 *
 * </pre>
 * @since 1.
 */
@Service
@Transactional
public class SportServiceImpl implements SportService {

	private Logger logger = LoggerFactory.getLogger(SportServiceImpl.class);

	@Autowired
	private SportDao sportDao;


	public boolean create(Sport sport){
		logger.info("单个新增：{}",sport);
		int count = sportDao.insert(sport);
		return count > 0;
	}


	public boolean update(Sport entity){
		logger.info("单个更新：{}", entity);
		int count = sportDao.updateByPrimaryKey(entity);
		return count > 0;
	}


	public boolean delete(Sport sport){
		logger.info("单个删除：{}", sport);
		int count = sportDao.deleteByPrimaryKey(sport);
		return count > 0;
	}

	public Sport findByPK(Integer id){
		Sport entity = new Sport();
		entity.setId(id);
		logger.info("单个查询：{}", entity);
		Sport sport = sportDao.select(entity);
		return sport;
	}


	public List<Sport> findAll(){
		Sport  entity = new  Sport();
		logger.info("查询所有：{}", entity);
		return sportDao.selectAll(entity);
	}


	public boolean deleteAll(List<Sport> list){
		logger.info("批量删除：{}", list);
		int count = sportDao.deleteBatch(list);
		return count > 0;
	}


	public boolean addBatch(List<Sport> list){
		logger.info("批量添加数据：{}",list);
		int count = sportDao.insertBatch(list);
		return count > 0;
	}

	public List<Sport> findLikeList(Sport sport){
		return sportDao.selectAll(sport);
	}


	//分页查询
	public List<Sport> findPage(int pageNum, int pageSize,Integer sportTypeId,Integer sortCondition){
		Sport  entity = new  Sport();
		entity.setPubSportTypeId(sportTypeId);
		entity.setSortCondition(sortCondition);//活动排序条件()
		logger.info("分页查询：{}");
		PageHelper.startPage(pageNum, pageSize);
		return sportDao.selectAll(entity);
	}

	public Sport findOne(Integer id,Integer userId){
	    Sport sport = sportDao.findOne(id);
//	    List<Integer> sportTypeIdList = sportDao.selectLoveSportTypeIdList(userId);
//	    List<Sport> sportList = sportDao.selectLoveSportList(sportTypeIdList);
	    sport.setBrowseNum(sport.getBrowseNum()+1);//浏览数加1
		sportDao.updateByPrimaryKey(sport);
		sportDao.insertUserLove(userId,sport.getPubSportTypeId());//记录用户的浏览记录
	    if(userId!=null){
	    	if(sport.getPubUserId()==userId){	//如果当前用户就是发布人
				sport.setSportFlag(Const.SportFlag.SPORT_FLAG_CABCEL_SPORT);
			}else {
				int resultNum = sportDao.selectUserSportRelationship(userId,id);
				if(resultNum>0){
					sport.setSportFlag(Const.SportFlag.SPORT_FLAG_CABCEL_JOIN);
				}
			}
        }
	    return sport;
	}

	/**
	 * 用户参加活动
	 * @param sport
	 * @param joinUserId
	 * @return
	 */
	public Integer joinSport(Sport sport,Integer joinUserId){
		Sport newSport = sportDao.findOne(sport.getId());
		if(newSport.getSportAllowJoinPersonNum()!=null
				&& newSport.getSportRealJoinPersonNum()>0
				&& newSport.getSportRealJoinPersonNum()==newSport.getSportAllowJoinPersonNum()){//数据库查看人数是否上限
			return Const.SportFlag.SPORT_FLAG_PERSON_FULL;
		}
		if(sportDao.selectUserSportRelationship(joinUserId,sport.getId())>0){
		    return Const.SportFlag.SPORT_FLAG_CABCEL_JOIN;
        }
		sport.setSportRealJoinPersonNum(sport.getSportRealJoinPersonNum()+1);
		sportDao.updateByPrimaryKey(sport);
		Integer sportId = sport.getId();
		sportDao.insertUserSportRelationship(joinUserId,sportId);
		sportDao.insertUserLove(joinUserId,sport.getPubSportTypeId());//记录用户的参加记录
		return Const.SUCCESS;
	}
	//取消参加活动
	public boolean CancleJoinSport(Sport sport,Integer joinUserId){
		Integer sportId = sport.getId();
		int count = sportDao.deleteUserSportRelationship(joinUserId,sportId);
		Sport sport1 = sportDao.select(sport);
		sport1.setSportRealJoinPersonNum(sport1.getSportRealJoinPersonNum()-1);
		count +=sportDao.updateByPrimaryKey(sport1);
		return count>0;
	}
	//取消活动
	public  boolean CancelSport(Integer id){
		Sport sport = new Sport();
		sport.setId(id);
		sport.setPubSportStatus(Const.SPORT_STATUS_CANCEL);
		int count = sportDao.updateByPrimaryKey(sport);
		return count>0;
	}

	public List<Sport> selectListByUserId(Integer userId){
		return sportDao.selectListByUserId(userId);
	}

	public List<Sport> selectIJoinSports(Integer userId){
		return sportDao.selectIJoinSports(userId);
	}

	public List<Sport> selectLoveSports(Integer userId){
		List<Integer> sportTypeIdList = sportDao.selectLoveSportTypeIdList(userId);
		List<Sport> sportList = new ArrayList<>();
		if(sportTypeIdList!=null && sportTypeIdList.size()>0){
			 sportList = sportDao.selectLoveSportList(sportTypeIdList);
		}

	    return  sportList;
	}
}